//index.js
import { request } from "../../request/request.js"
//获取应用实例
const app = getApp()
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "H+": this.getHours(), //小时 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

Page({
  data: {
    CheckType:'',
    index: null,
    checkCategory1: ['日', '周', '月'],
    checkCategory:'',
    checkDate: new Date().Format("yyyy-MM-dd"),
    checkContent1:['HSE专项检查内容','质量专项检查内容'],
    checkContent:'',
    companyName:'',
    company_list :[],
    companyCode:'',
    checklist: [{
      "id": "01",
      "nodeCode": "01",
      "label": "HSE专项检查内容",
    },
      {
        "id": "02",
        "nodeCode": "02",
        "label": "质量专项检查内容",
      },
    ],
    unnormalizedValue2:'',
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
//选择检查类型
  PickerChange(e) {
    var that=this
    console.log(e);
    this.setData({
      index: e.detail.value,
    })
    that.data.checkCategory= that.data.checkCategory1[e.detail.value]
  },
//输入受检时间
  DateChange(e) {
    console.log(e);
    this.setData({
      checkDate: e.detail.value
    })
  },
 showRule: function () {
    this.setData({
      isRuleTrue: true
    })
  },
  hideRule: function () {
    this.setData({
      isRuleTrue: false
    })
  },
  selThis(e) {
    this.setData({
      companyName: e.detail.label,
      companyCode: e.detail.nodeCode,
      isRuleTrue: false
    })
  },
  //检查内容
  showRule1: function () {
    this.setData({
      isRuleTrue1: true
    })
  },
  hideRule1: function () {
    this.setData({
      isRuleTrue1: false
    })
  },
  selThis1(e) {
    this.setData({
      checkContent: e.detail.label,
      isRuleTrue1: false
    })
  },

  onLoad: function (options) {
    
    var that = this;
    //获取检查类别
    var checkType=options.CheckType
    that.setData({
      CheckType:checkType
    })
    console.log("检查类别="+ this.data.CheckType)
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    // 获取公司列表
    this.getCompanyList();
  },

  async getCompanyList(){
    let  token=wx.getStorageSync('token')
    console.log(token)
    const header = {Authorization:token};
    const res= await request({url:'/company/tree',data:{},header:header});
    console.log(res);
    this.setData({
      unnormalizedValue2: res.data.data
    })
  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //提交表格
  submitClick: function () {
    if (this.data.checkCategory == ''){
      this.data.checkCategory ='日'
    }
    if (this.data.checkContent==''){
      this.data.checkContent ='HSE专项检查内容'
    }
    wx.navigateTo({
      url: '../Checklist/Checklist?CheckType='+this.data.CheckType+'&checkCategory='+this.data.checkCategory+'&companyCode='+this.data.companyCode+'&companyName='+this.data.companyName+'&checkDate='+this.data.checkDate+'&checkContent='+ this.data.checkContent,
    })
  },
})
